package pers.duorhs.grgbusstation.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import pers.duorhs.grgbusstation.amap.AmapApiHelper;
import pers.duorhs.grgbusstation.domain.GeoLocation;
import pers.duorhs.grgbusstation.domain.StationInfo;
import pers.duorhs.grgbusstation.mapper.StationsMapper;
import pers.duorhs.grgbusstation.service.StationsService;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Duorhs
 * @date 2022/7/26 10:04
 */
@Service
@AllArgsConstructor
@Slf4j
public class StationsServiceImpl extends ServiceImpl<StationsMapper, StationInfo> implements StationsService {

    private final AmapApiHelper apiHelper;

    @PostConstruct
    void initList() {
    }

    /**
     * 返回所有站点
     */
    @Override
    @Cacheable(cacheNames = "stationList")
    public List<StationInfo> getAll() {
        return this.list();
    }

    /**
     * TODO 开发用
     * 读取班车EXCEL，构造站点信息列表
     *
     * @return 返回站点信息
     */
    @Override
    public void DEV_READ_EXCEL() {
        List<StationInfo> stationInfos = new ArrayList<>();
        LinkedHashMap<String, DEV_OBJ> tmpMap = new LinkedHashMap<>();
        //读取excel班车表，存入列表
        File file = new File("stations/excel/班车线路表.xlsx");
        ExcelReader excelReader = ExcelUtil.getReader(file, "总表");
        List<Map<String, Object>> all = excelReader.readAll();
        for (Map<String, Object> map : all) {
            StationInfo stationInfo = new StationInfo();
            stationInfo.setId(Integer.valueOf(String.valueOf(map.get("序号"))));
            stationInfo.setLine(String.valueOf(map.get("线路名称")));
            String type = String.valueOf(map.get("班车类型"));
            stationInfo.setType(type);
            stationInfo.setRunStatus(String.valueOf(map.get("线路状态")));
            stationInfo.setName(String.valueOf(map.get("站点")));
            stationInfo.setRunTime(String.valueOf(map.get("时间")));


            //使用高德API获取班车信息
            String briefName = String.valueOf(map.get("简名"));
            stationInfo.setBriefName(briefName);
            //利用缓存避免多次重复调用API查询
            if (map.containsKey(briefName)) {
                stationInfo.setGeoName(tmpMap.get(briefName).geoName);
                stationInfo.setGeoLocation(tmpMap.get(briefName).geoLocation);
            } else {
                List<GeoLocation> geoInfo = apiHelper.apiGeoInfo(stationInfo.getBriefName(), "广州");
                if (geoInfo.size() != 0) {
                    String geoName = geoInfo.get(0).getName();
                    String geoLocation = geoInfo.get(0).getGeoLocation();
                    tmpMap.put(briefName, new DEV_OBJ(geoName, geoLocation));
                    stationInfo.setGeoName(geoName);
                    stationInfo.setGeoLocation(geoLocation);
                }
            }
            stationInfos.add(stationInfo);
        }
        this.saveBatch(stationInfos);
    }

    @AllArgsConstructor
    private class DEV_OBJ {
        String geoName;
        String geoLocation;
    }
}
